﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BL_Server
{
    public class BL : IBL
    {
        DAL.IDAL dal;

        public BL()
        {
            dal = new DAL.DAL();
        }

        #region Cities
        public List<BE.Model.City> GetAllCities()
        {
            return dal.GetAllCities().ToList();
        }

        public bool AddCity(BE.Model.City city)
        {
            return dal.AddCity(city);
        }

        public bool RemoveCity(int id)
        {
            return dal.RemoveCity(id);
        }

        public bool UpdateCity(BE.Model.City city)
        {
            return dal.UpdateCity(city);
        }
        #endregion

        #region Professions
        public List<BE.Model.Profession> GetAllProfessions()
        {
            return dal.GetAllProfessions().ToList();
        }

        public bool AddProfession(BE.Model.Profession profession)
        {
            return dal.AddProfession(profession);
        }

        public bool RemoveProfession(int id)
        {
            return dal.RemoveProfession(id);
        }

        public bool UpdateProfession(BE.Model.Profession profession)
        {
            return dal.UpdateProfession(profession);
        }
        #endregion

        #region Programs
        public List<BE.Model.Program> GetAllPrograms()
        {
            return dal.GetAllPrograms().ToList();
        }

        public bool AddProgram(BE.Model.Program program)
        {
            return dal.AddProgram(program);
        }

        public bool RemoveProgram(int id)
        {
            return dal.RemoveProgram(id);
        }

        public bool UpdateProgram(BE.Model.Program program)
        {
            return dal.UpdateProgram(program);
        }
        #endregion

        #region Dorms
        public List<BE.Model.Dorm> GetAllDorms()
        {
            return dal.GetAllDorms().ToList();
        }

        public bool AddDorm(BE.Model.Dorm dorm)
        {
            return dal.AddDorm(dorm);
        }

        public bool RemoveDorm(int ID)
        {
            return dal.RemoveDorm(ID);
        }

        public bool UpdateDorm(BE.Model.Dorm dorm)
        {
            return dal.UpdateDorm(dorm);
        }
        #endregion

        #region Rooms
        public List<BE.Model.Room> GetAllRooms()
        {
            return dal.GetAllRooms().ToList();
        }

        public bool AddRoom(BE.Model.Room room)
        {
            return dal.AddRoom(room);
        }

        public bool RemoveRoom(int id)
        {
            return dal.RemoveRoom(id);
        }

        public bool UpdateRoom(BE.Model.Room room)
        {
            return dal.UpdateRoom(room);
        }
        #endregion

        #region Charges
        public IEnumerable<BE.Model.Charge> GetAllCharges()
        {
            return dal.GetAllCharges().ToList();
        }

        public bool AddCharge(BE.Model.Charge charge)
        {
            return dal.AddCharge(charge);
        }

        public bool RemoveCharge(int id)
        {
            return dal.RemoveCharge(id);
        }

        public bool UpdateCharge(BE.Model.Charge charge)
        {
            return dal.UpdateCharge(charge);
        }
        #endregion

        #region Students
        public IEnumerable<BE.Model.Student> GetAllStudents()
        {
            return dal.GetAllStudents().ToList();
        }

        public bool AddStudent(BE.Model.Student student)
        {
            return dal.AddStudent(student);
        }

        public bool RemoveStudent(int id)
        {
            return dal.RemoveStudent(id);
        }

        public bool UpdateStudent(BE.Model.Student student)
        {
            return dal.UpdateStudent(student);
        }
        #endregion

        #region Placements
        public IEnumerable<BE.Model.Placement> GetAllPlacements()
        {
            return dal.GetAllPlacements().ToList();
        }

        public bool AddPlacement(BE.Model.Placement placement)
        {
            BE.Model.Room inRoom = dal.GetAllRooms().Where(r => r.ID == placement.RoomID).First();
            
            if (placement.Departure < placement.Enterance ||
                placement.ChargeEnd < placement.ChargeStart)
                return false;

            if (dal.GetAllPlacements().Where(p => p.Room.ID == inRoom.ID).Count() >= inRoom.NumberOfBeds)
                return false;

            if (dal.GetAllPlacements().Where(p => p.StudentID == placement.StudentID).Count() > 0)
                return false;

            return dal.AddPlacement(placement);
        }

        public bool RemovePlacement(int id)
        {
            return dal.RemovePlacement(id);
        }

        public bool UpdatePlacement(BE.Model.Placement placement)
        {
            BE.Model.Room inRoom = dal.GetAllRooms().Where(r => r.ID == placement.RoomID).First();

            if (placement.Departure < placement.Enterance ||
                placement.ChargeEnd < placement.ChargeStart)
                return false;

            if (dal.GetAllPlacements().Where(p => p.Room.ID == inRoom.ID).Count() >= inRoom.NumberOfBeds)
                return false;

            if (dal.GetAllPlacements().Where(p => p.StudentID == placement.StudentID).Count() > 1)
                return false;

            return dal.UpdatePlacement(placement);
        }
        #endregion

        #region Users
        public IEnumerable<BE.Model.User> GetAllUsers()
        {
            return dal.GetAllUsers().ToList();
        }

        public bool AddUser(BE.Model.User user)
        {
            return dal.AddUser(user);
        }

        public bool RemoveUser(int id)
        {
            return dal.RemoveUser(id);
        }

        public bool UpdateUser(BE.Model.User user)
        {
            return dal.UpdateUser(user);
        }

        public BE.Model.User GetUserByID(int id)
        {
            return dal.GetUserByID(id);
        }

        public BE.Model.User GetUserByEmail(string email)
        {
            return dal.GetUserByEmail(email);
        }
        #endregion   

        
    }
}
